#include "cilib.h"
void matrixProcess2(void *ptr,int k,int error)
{
    int w,h;
    int i,j;
    int m,n;
    PIXEL* last;
    PIXEL* simg;
    PIXEL* dimg;

    int r,g,b;
    int skip;

    void *p2=malloc(ciGetFileSize(ptr)*sizeof(BYTE));

    memcpy(p2,ptr,ciGetFileSize(ptr));

    ciGetImageSize(ptr,&w,&h);
    simg=ciGetImageArray(ptr);
    dimg=ciGetImageArray(p2);

    for(i=0;i<w-k+1;++i){
        for(j=0;j<h-k+1;++j){
            last=simg+i+j*w;
            r=0;
            g=0;
            b=0;
            skip=0;

            for(m=0;m<k;++m){
                for(n=0;n<k;++n){
                    if((abs((simg+(i+m)+(j+n)*w)->r-last->r)<=error)
                        &&(abs((simg+(i+m)+(j+n)*w)->g-last->g)<=error)
                        &&(abs((simg+(i+m)+(j+n)*w)->b-last->b)<=error)){
                          r+=(simg+(i+m)+(j+n)*w)->r;
                          g+=(simg+(i+m)+(j+n)*w)->g;
                          b+=(simg+(i+m)+(j+n)*w)->b;
                    }else{
                        skip=1;
                    }
                    last=(simg+(i+m)+(j+n)*w) ;
                    if(skip){
                        m=k;
                        n=k;
                    }
                }
            }

            if(!skip){
                for(m=0;m<k;++m){
                    for(n=0;n<k;++n){
                          (dimg+(i+m)+(j+n)*w)->r=r / (float)(k*k);
                          (dimg+(i+m)+(j+n)*w)->g=g / (float)(k*k);
                          (dimg+(i+m)+(j+n)*w)->b=b / (float)(k*k);
                    }
                }
            }
        }
    }
    memcpy(ptr,p2,ciGetFileSize(ptr));

}



void matrixProcess(void *ptr,int k,int error)
{
    int w,h;
    int i,j;
    int m,n;
    PIXEL* last;
    PIXEL* simg;
    PIXEL* dimg;

    int r,g,b;
    int skip;


    ciGetImageSize(ptr,&w,&h);
    simg=ciGetImageArray(ptr);
    dimg=simg;

    for(i=0;i<w-k+1;++i){
        for(j=0;j<h-k+1;++j){
            last=simg+i+j*w;
            r=0;
            g=0;
            b=0;
            skip=0;

            for(m=0;m<k;++m){
                for(n=0;n<k;++n){
                    if((abs((simg+(i+m)+(j+n)*w)->r-last->r)<=error)
                        &&(abs((simg+(i+m)+(j+n)*w)->g-last->g)<=error)
                        &&(abs((simg+(i+m)+(j+n)*w)->b-last->b)<=error)){
                          r+=(simg+(i+m)+(j+n)*w)->r;
                          g+=(simg+(i+m)+(j+n)*w)->g;
                          b+=(simg+(i+m)+(j+n)*w)->b;
                    }else{
                        skip=1;
                    }
                    last=(simg+(i+m)+(j+n)*w) ;
                    if(skip){
                        m=k;
                        n=k;
                    }
                }
            }

            if(!skip){
                for(m=0;m<k;++m){
                    for(n=0;n<k;++n){
                          (dimg+(i+m)+(j+n)*w)->r=r / (float)(k*k);
                          (dimg+(i+m)+(j+n)*w)->g=g / (float)(k*k);
                          (dimg+(i+m)+(j+n)*w)->b=b / (float)(k*k);
                    }
                }
            }
        }
    }


}





